package cn.ainit.sys.yzpe;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ainit.sys.menu.SessionCont;


@Controller
@RequestMapping("/sysyzpe")
public class YzpeController {
	
	private YzpeService yzpeService;
	
	@Resource
	public void setYzpeService(YzpeService yzpeService) {
		this.yzpeService = yzpeService;
	}
	
	@RequestMapping("/caclu")
	@ResponseBody
	public YzpeReturnObj calculate(@RequestBody YzpeReceiveObj receiveObj,HttpServletRequest request){
		YzpeReturnObj returnObj = new YzpeReturnObj();
		try {
			if (receiveObj == null || receiveObj.getFuncCode() == null 
					|| "".equals(receiveObj.getFuncCode()) ) {
				throw new RuntimeException("没有传入方法编码");
			}
			returnObj = yzpeService.calculate(receiveObj);
			
		} catch (Exception e) {
			e.printStackTrace();
			returnObj.setMsg(e.getMessage());
		}
		return returnObj;
	}
	/**
	 * 印刷付印单 添加版印次
	 * @param djId
	 * @param allObj
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveNewPICI")
	@ResponseBody
    public List<String> saveNewPICI(String djId,@RequestBody List<Map<String, String>> allObj,HttpServletRequest request,HttpSession session) throws Exception{
    	List<String> listStr = new ArrayList<String>();
		String flag = "";
		try {
			Object uid = session.getAttribute(SessionCont.uid);
			if (uid == null || "".equals(uid.toString())) {
				System.out.println("用户未登录 SingleController L.138");
			}
			flag = this.yzpeService.newPiciFunc(djId,allObj,uid.toString());
			listStr.add(flag);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listStr;
    }
	/**
	 * 印刷付印单 添加版印次前的判断过程
	 * @param djId
	 * @param allObj
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/pdNewPICI")
	@ResponseBody
    public List<String> pdNewPICI(String peId,String dsId,String xtid,HttpServletRequest request,HttpSession session) throws Exception{
    	List<String> listStr = new ArrayList<String>();
		String flag = "";
		try {
			Object uid = session.getAttribute(SessionCont.uid);
			if (uid == null || "".equals(uid.toString())) {
				System.out.println("用户未登录 SingleController L.138");
			}
			flag = this.yzpeService.judgePiciFunc(peId, uid.toString(), dsId);
			String queryzrr=null;
			try {
				
				  queryzrr=yzpeService.queryZRR(xtid);
			} catch (Exception e) {
				// TODO: handle exception
			}
			listStr.add(flag);
			listStr.add(queryzrr);
		} catch (Exception e) {
			e.printStackTrace();
		}
			
		return listStr;
    }
}
